package com.qiaofang.housewarehouse.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.qiaofang.housewarehouse.base.util.BeanUtil;
import com.qiaofang.housewarehouse.dao.ESSearchHouseDao;
import com.qiaofang.housewarehouse.dao.EsDAO;
import com.qiaofang.housewarehouse.dao.HbaseDao;
import com.qiaofang.housewarehouse.dao.domain.EsHswhRentalHouse;
import com.qiaofang.housewarehouse.dao.domain.EsHswhSaleHouse;
import com.qiaofang.housewarehouse.dao.domain.HswhRentalHouse;
import com.qiaofang.housewarehouse.dao.domain.HswhSaleHouse;
import com.qiaofang.housewarehouse.dao.utils.ConfigUtils;
import com.qiaofang.housewarehouse.service.RentalHouseListQueryService;
import com.qiaofang.housewarehouse.service.compose.HswhRentalHouseCompose;
import com.qiaofang.housewarehouse.service.compose.HswhSaleHouseCompose;
import com.qiaofang.housewarehouse.stub.bean.HousingAcqAttentionDTO;
import com.qiaofang.housewarehouse.stub.bean.HswhRentalHouseDetailDTO;
import com.qiaofang.housewarehouse.stub.bean.HswhRentalHouseListEntity;
import com.qiaofang.housewarehouse.stub.bean.HswhRentalHouseQueryResponseDTO;
import com.qiaofang.housewarehouse.stub.bean.HswhRentalHouseQueryResponseEntity;
import com.qiaofang.housewarehouse.stub.bean.HswhSaleHouseDetailDTO;
import com.qiaofang.housewarehouse.stub.constants.TableConstants;
import org.apache.ibatis.logging.LogFactory;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Author: GJ
 * @Date: 2018/4/4 10:34
 * @Description:
 */
@Service
public class RentalHouseListQueryServceImpl implements RentalHouseListQueryService {

    private static final Logger logger = LoggerFactory.getLogger(RentalHouseListQueryServceImpl.class);
    @Resource
    private HbaseDao hbaseDao;
    @Resource
    private EsDAO esDAO;
    @Resource
    private ESSearchHouseDao essDao;

    @Override
    public HswhRentalHouseQueryResponseEntity getRentalHouseList(Map<String, String> paramMap) throws Exception {
        paramMap.put("indexName", ConfigUtils.getIndexHswhRentalHouse());
        paramMap.put("typeName", TableConstants.INDEX_TYPE_RENTAL_HOUSE);
        logger.info("paramMap：{}", paramMap.toString());
        HswhRentalHouseQueryResponseEntity hswhRentalHouseQueryResponseEntity = new HswhRentalHouseQueryResponseEntity();
        SearchResponse searchResponse = esDAO.searchHouseList(paramMap);
        logger.info("查询eslticsearch success");
        if (searchResponse != null) {
            SearchHits hits = searchResponse.getHits();
            SearchHit[] hits1 = hits.getHits();
            List<EsHswhRentalHouse> rentalHouses = new ArrayList<>();
            for (SearchHit hit : hits1) {
                Map<String, Object> source = hit.getSource();
                EsHswhRentalHouse hswhRentalHouse = BeanUtil.mapperObj(source, EsHswhRentalHouse.class);
                hswhRentalHouse.setId(hit.getId());
                rentalHouses.add(hswhRentalHouse);
            }
 //           logger.info("从es中获取的二级索引：{}", rowkeys.toString());
  //          List<HswhRentalHouse> hswhRentalHouses = hbaseDao.listObjByRowKeys(TableConstants.HSWH_RENTAL_HOUSE, rowkeys, HswhRentalHouse.class);
            hswhRentalHouseQueryResponseEntity.setHswhRentalHouseListEntities(HswhRentalHouseCompose.composeListEntity1(rentalHouses));
            if(rentalHouses.size() == 0){
                hswhRentalHouseQueryResponseEntity.setTotalCount(String.valueOf(0));
            }else{
                hswhRentalHouseQueryResponseEntity.setTotalCount(String.valueOf(hits.getTotalHits()));
            }
            hswhRentalHouseQueryResponseEntity.setPageIndex(paramMap.get("pageIndex"));
        }
        return hswhRentalHouseQueryResponseEntity;
    }

    @Override
    public HswhRentalHouseQueryResponseEntity getRentalHouseList(Map<String, String> paramMap, List<HousingAcqAttentionDTO> housingAcqAttentionDTOS) throws Exception {
        paramMap.put("indexName", ConfigUtils.getIndexHswhRentalHouse());
        paramMap.put("typeName", TableConstants.INDEX_TYPE_RENTAL_HOUSE);
        logger.info("paramMap：{}", paramMap.toString());
        HswhRentalHouseQueryResponseEntity hswhRentalHouseQueryResponseEntity = new HswhRentalHouseQueryResponseEntity();
        SearchResponse searchResponse = essDao.searchHouseList(paramMap,housingAcqAttentionDTOS,"rent");
        logger.info("查询eslticsearch success");
        if (searchResponse != null) {
            SearchHits hits = searchResponse.getHits();
            SearchHit[] hits1 = hits.getHits();
            List<EsHswhRentalHouse> rentalHouses = new ArrayList<>();
            for (SearchHit hit : hits1) {
                Map<String, Object> source = hit.getSource();
                EsHswhRentalHouse hswhRentalHouse = BeanUtil.mapperObj(source, EsHswhRentalHouse.class);
                hswhRentalHouse.setId(hit.getId());
                rentalHouses.add(hswhRentalHouse);
            }
     //       logger.info("从es中获取的二级索引：{}", rowkeys.toString());
    //        List<HswhRentalHouse> hswhRentalHouses = hbaseDao.listObjByRowKeys(TableConstants.HSWH_RENTAL_HOUSE, rowkeys, HswhRentalHouse.class);
            hswhRentalHouseQueryResponseEntity.setHswhRentalHouseListEntities(HswhRentalHouseCompose.composeListEntity1(rentalHouses));
            if(rentalHouses.size() == 0){
                hswhRentalHouseQueryResponseEntity.setTotalCount(String.valueOf(0));
            }else{
                hswhRentalHouseQueryResponseEntity.setTotalCount(String.valueOf(hits.getTotalHits()));
            }
            hswhRentalHouseQueryResponseEntity.setPageIndex(paramMap.get("pageIndex"));
        }
        return hswhRentalHouseQueryResponseEntity;
    }

    @Override
    public List<HswhRentalHouseDetailDTO> listRentalHouseDetailByIds(Map<String, String> paramMap,String tableName,
        String
        ids, Class cls) throws Exception {
        paramMap.put("ids", ids);
        SearchResponse searchResponse = esDAO.searchHouseList(paramMap);
        logger.info("查询eslticsearch succee");
        List<HswhRentalHouse> hswhRentalHouses = this.hits2Json(searchResponse.getHits());
        return HswhRentalHouseCompose.composeDetailListDTO(hswhRentalHouses);
    }
    private List<HswhRentalHouse> hits2Json(SearchHits hits){
        List<HswhRentalHouse> hswhRentalHouses = new ArrayList<>();
        if (hits.getTotalHits() != 0) {
            for (SearchHit searchHit : hits.getHits()) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("id", searchHit.getId());
                Map<String, Object> source = searchHit.getSource();
                for (String key : source.keySet()) {
                    jsonObject.put(key, source.get(key));
                }
                hswhRentalHouses.add(JSONObject.parseObject(jsonObject.toJSONString(), HswhRentalHouse.class));
            }
        }
        return hswhRentalHouses;
    }
}
